package pers.sloera.nowcoder.length;

import java.util.*;
import java.util.stream.Collectors;

/**
 * class pers.sloera.nowcoder.length
 * user sloera
 * date 2022/3/6
 * 85
 */
public class Main {
    public static void main(String[] args) {
        // AAAAHHHBBCDHHHH
        // 3
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = null;
        if (in.hasNextLine()) {
            str = in.nextLine();
        }
        int order = 0;
        if (in.hasNextInt()) {
            order = in.nextInt();
        }
        try {
            resolve(str, order);
        } catch (Exception e) {
            System.out.println(-1);
        }
    }

    private static void resolve(String str, int order) {
        // 最长长度
        final HashMap<Character, Integer> maxLengthOfAlphabet = new HashMap<>();
        // 总和
        final HashMap<Character, Integer> sumOfAlphabet = new HashMap<>();
        char last = 0;
        // 计算临时长度
        int temp = 0;
        for (int i = 0; i < str.length(); i++) {
            final char c = str.charAt(i);
            sumOfAlphabet.put(c, Optional.ofNullable(sumOfAlphabet.get(c)).orElse(0) + 1);
            if (i == 0) {
                maxLengthOfAlphabet.put(c, 1);
            }
            // 计算下一个
            if (i > 0 && c != str.charAt(i - 1)) {
                final Integer lastLength = Optional.ofNullable(maxLengthOfAlphabet.get(str.charAt(i - 1))).orElse(0);
                maxLengthOfAlphabet.put(str.charAt(i - 1), Math.max(temp, lastLength));
                maxLengthOfAlphabet.put(str.charAt(i), 1);
                temp = 1;
                continue;
            }
            temp++;
        }
        // 避免最后一位未被计算到
        if (temp != 0) {
            final Integer lastLength = Optional.ofNullable(maxLengthOfAlphabet.get(str.charAt(str.length() - 1))).orElse(0);
            maxLengthOfAlphabet.put(str.charAt(str.length() - 1), Math.max(temp, lastLength));
        }
        final List<Integer> collect = maxLengthOfAlphabet.values().stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        final Integer integer;
        try {
            integer = collect.get(order - 1);
        } catch (Exception e) {
            System.out.println(-1);
            return;
        }
        maxLengthOfAlphabet.entrySet().stream().filter(entry -> entry.getValue().equals(integer)).findFirst().ifPresent(entry -> {
            // System.out.println(sumOfAlphabet.get(entry.getKey()));
            System.out.println(entry.getValue());
        });
    }
}
